package com.lry.config.condition;

import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class PropertiesEnableCondition extends SpringBootCondition {

    protected final String propName;

    protected final boolean matchIfMissing;

    public PropertiesEnableCondition(String propName,boolean matchIfMissing){
        this.propName = propName;
        this.matchIfMissing = matchIfMissing;
    }

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Boolean enable = context.getEnvironment().getProperty(propName,Boolean.class);
        if ((enable == null && matchIfMissing) || (enable != null && enable))
            return ConditionOutcome.match("开启:"+propName);
        return ConditionOutcome.noMatch("不开启:"+propName);
    }

}
